package com.hzzc.core.service;

import com.hzzc.BizException;
import com.hzzc.api.SSOResourceTreeView;
import com.hzzc.core.dao.SSOResourceMapper;
import com.hzzc.core.model.SSOResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zmm
 * 资源管理服务
 */
@Service
public class ResourceService {

    @Autowired
    private SSOResourceMapper ssoResourceMapper;


    /**
     * 获取属于该应用的指定父级的子集资源
     *
     * @param appId
     * @return 当应用id为null时，忽略app的条件限制
     */
    public List<SSOResource> getAppResources(Long appId, Long parentId) {
        Example example = new Example(SSOResource.class);
        example.orderBy("id").desc();
        if (parentId == null) {
            parentId = 0L;
        }
        example.createCriteria().andEqualTo("applicationId", appId).andEqualTo("parentResourceId", parentId);
        return ssoResourceMapper.selectByExample(example);
    }

    /**
     * 增加资源
     *
     * @param name
     * @param appId
     * @param msg
     * @param actionFlag
     * @throws BizException
     */
    public void addResource(String name, Long appId, Long parentId, String locator, String code, String msg, boolean actionFlag) throws BizException {
        SSOResource ssoResource = new SSOResource();
        ssoResource.setApplicationId(appId);
        ssoResource.setCode(code);
        ssoResource.setLocator(locator);
        ssoResource.setMsg(msg);
        ssoResource.setName(name);
        ssoResource.setParentResourceId(parentId);
        ssoResource.setActionFlag(actionFlag);
        ssoResourceMapper.insert(ssoResource);
    }

    /**
     * 用户应用对应的资源树
     *
     * @param appId
     * @return
     */
    public List<SSOResourceTreeView> getAppResourceView(Long appId) {
        Example example = new Example(SSOResource.class);
        example.orderBy("id").asc();
        example.createCriteria().andEqualTo("applicationId", appId);
        List<SSOResource> ssoResources = ssoResourceMapper.selectByExample(example);
        return createResourceTreeView(ssoResources);
    }

    /**
     * 初始化权限树
     *
     * @param ssoResources
     * @return
     */
    public List<SSOResourceTreeView> createResourceTreeView(List<SSOResource> ssoResources) {
        List<SSOResource> rootResources = ssoResources.stream().filter(sr -> sr.getParentResourceId() == 0L).collect(Collectors.toList());
        List<SSOResourceTreeView> SSOResourceTreeViews = new ArrayList<>();
        for (SSOResource rootResource : rootResources) {
            SSOResourceTreeView SSOResourceTreeView = new SSOResourceTreeView().setId(rootResource.getId()).setLocator(rootResource.getLocator()).setCode(rootResource.getCode()).setMsg(rootResource.getMsg()).setName(rootResource.getName()).setActionFlag(rootResource.getActionFlag()).setApplicationId(rootResource.getApplicationId()).setParentId(rootResource.getParentResourceId());
            fillChildResources(SSOResourceTreeView, ssoResources);
            SSOResourceTreeViews.add(SSOResourceTreeView);
        }
        return SSOResourceTreeViews;
    }

    /**
     * 填充子集目录
     *
     * @param SSOResourceTreeView
     * @param ssoResources
     */
    private void fillChildResources(SSOResourceTreeView SSOResourceTreeView, List<SSOResource> ssoResources) {
        long parentId = SSOResourceTreeView.getId();
        List<SSOResource> children = ssoResources.stream().filter(sr -> sr.getParentResourceId() == parentId).collect(Collectors.toList());
        List<SSOResourceTreeView> childrenView = new ArrayList<>();
        if (children != null && children.size() > 0) {
            for (SSOResource child : children) {
                SSOResourceTreeView childView = new SSOResourceTreeView().setId(child.getId()).setLocator(child.getLocator()).setCode(child.getCode()).setMsg(child.getMsg()).setName(child.getName()).setActionFlag(child.getActionFlag()).setApplicationId(child.getApplicationId()).setParentId(child.getParentResourceId());
                childrenView.add(childView);
                fillChildResources(childView, ssoResources);
            }
            SSOResourceTreeView.setSsoResourceTreeViews(childrenView);
        }
    }

    /**
     * 删除资源
     *
     * @param id
     */
    public void delResource(Long id) {
        ssoResourceMapper.deleteByPrimaryKey(id);
    }

    public SSOResource selectByLocatorAndAppId(String locator, Long appId) {
        Example example = new Example(SSOResource.class);
        example.createCriteria().andEqualTo("application_id", appId).andEqualTo("locator", locator);
        return ssoResourceMapper.selectOneByExample(example);
    }

    public void updateResource(Long id, String name, Long appId, Long parentId, String locator, String code, String msg, boolean actionFlag) {
        SSOResource ssoResource = new SSOResource();
        ssoResource.setId(id);
        ssoResource.setApplicationId(appId);
        ssoResource.setCode(code);
        ssoResource.setLocator(locator);
        ssoResource.setMsg(msg);
        ssoResource.setName(name);
        ssoResource.setParentResourceId(parentId);
        ssoResource.setActionFlag(actionFlag);
        ssoResourceMapper.updateByPrimaryKey(ssoResource);
    }

    public List<SSOResource> selectByIds(Set<Long> resourceIds) {
        Example ssoResource = new Example(SSOResource.class);
        ssoResource.orderBy("id").asc();
        ssoResource.createCriteria().andIn("id", resourceIds);
        return ssoResourceMapper.selectByExample(ssoResource);
    }
}
